package appx.craft.droid.uc;

import java.util.ArrayList;
import java.util.LinkedList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import appx.craft.droid.R;
import appx.craft.droid.bean.CommentBeans;
import appx.craft.droid.utils.Log;

public class CommentsAdapters extends BaseAdapter {
	
	private final String TAG = CommentsAdapters.class.getCanonicalName();
	
	private LinkedList<CommentBeans> mcommentsList;
	private LayoutInflater mInflater;
	
	public CommentsAdapters(Context mContext){
		Log.d(TAG,"[ CommentsAdapters ]");
		mInflater = LayoutInflater.from(mContext);
		mcommentsList = new LinkedList<CommentBeans>();
	}
	
	public void addNewCommentsLists(ArrayList<CommentBeans> mCommentsList){
		
		if(mcommentsList != null){
			
			if(!mcommentsList.isEmpty()){
				mcommentsList.clear();
			}
			
			for(CommentBeans mTempBean : mCommentsList){
				mcommentsList.addFirst(mTempBean);
			}
			notifyDataSetChanged();
		}
	}
	
	public void addNewComments(CommentBeans mTempBean){
		if(mcommentsList != null){
			// remove first view before we added
			loadMoreView(false);
			mcommentsList.addFirst(mTempBean);
			notifyDataSetChanged();
		}
	}
	
	@Override
	public int getCount() {
		return mcommentsList.size();
	}

	@Override
	public Object getItem(int position) {
		return mcommentsList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		ViewHolder mViewHolder;
		
		if(convertView == null){
			
			mViewHolder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.row_item_comments, parent, false);
			mViewHolder.mCommentViewGroup = (LinearLayout)convertView.findViewById(R.id.layout_comments);
			mViewHolder.mLoadViewGroup = (LinearLayout)convertView.findViewById(R.id.layout_load_more);
			mViewHolder.mImageViewProfile = (ImageView)convertView.findViewById(R.id.imageView_comments_people);
			mViewHolder.mTextViewName = (TextView)convertView.findViewById(R.id.textView_name);
			mViewHolder.mTextViewCaption = (TextView)convertView.findViewById(R.id.textView_comment_caption);
			
			// catch view 
			convertView.setTag(mViewHolder);
		}else{
			
			mViewHolder = (ViewHolder)convertView.getTag();
		}
		
		final CommentBeans mTempComments = mcommentsList.get(position);
		
		if(mTempComments != null){
			if(mTempComments.isHeaderView){
				mViewHolder.mLoadViewGroup.setVisibility(View.VISIBLE);
				final Button btnLoadMore = (Button)mViewHolder.mLoadViewGroup.findViewById(R.id.btn_load_more);
				btnLoadMore.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						loadMoreComments(5);
						if(mcommentsList.size() > 11){
							// when ever you added new item once you know then is more item still then show load more usage below function
							loadMoreView(false);
						}else{
							loadMoreView(true);
						}

						
					}
				});
				mViewHolder.mCommentViewGroup.setVisibility(View.GONE);
			}else{
				mViewHolder.mLoadViewGroup.setVisibility(View.GONE);
				mViewHolder.mCommentViewGroup.setVisibility(View.VISIBLE);
				mViewHolder.mTextViewName.setText(mTempComments.getStrName());
				mViewHolder.mTextViewCaption.setText(mTempComments.getStrCaptions());	
			}
			
		}
		
		return convertView;
	}
	
	class ViewHolder {
		ViewGroup mCommentViewGroup;
		ViewGroup mLoadViewGroup;
		TextView mTextViewName;
		TextView mTextViewCaption;
		ImageView mImageViewProfile;
	}
	
	public void loadMoreView(boolean flag){
		if (flag) {
			if (!mcommentsList.get(0).isHeaderView()) {
				
				CommentBeans mHeaderBean = new CommentBeans();
				mHeaderBean.setHeaderView(true);
				mcommentsList.add(0,mHeaderBean);
				notifyDataSetChanged();
			}

		} else {
			
			if (mcommentsList.get(0).isHeaderView()) {
				mcommentsList.remove(0);
				notifyDataSetChanged();
			}
		}
	}
	
	public void loadMoreComments(int number){
		CommentBeans mTempBeans;
		for(int i=0;i<number;i++){
			mTempBeans = new CommentBeans();
			mTempBeans.setStrCaptions("Nice");
			mTempBeans.setStrName("ABC Test");
			addNewComments(mTempBeans);
		}
	}
}
